
using System;

namespace System.Web.HttpHandlers
{
    /// <summary>
    /// Compression HttpHandler
    /// </summary>
    public class CompressionHandler : System.Web.IHttpHandler
    {
        #region private object
        private const string GZIP = "gzip";
        private const string DEFLATE = "deflate";
        #endregion

        #region IHttpHandler
        /// <summary>
        /// true
        /// </summary>
        public bool IsReusable
        {
            get { return true; }
        }
        /// <summary>
        /// process request
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(System.Web.HttpContext context)
        {
            string filepath = context.Request.FilePath.ToLower();

            if (!filepath.EndsWith(".js"))

                filepath = filepath.Substring(0, filepath.LastIndexOf("."));

            filepath = context.Server.MapPath(filepath);


            if (context.IsEncodingAccepted(GZIP))
            {
                filepath += ".gz";

                context.SetEncoding(GZIP);
            }
            else
            {
                if (context.IsEncodingAccepted(DEFLATE))
                {
                    filepath += ".de";
                    context.SetEncoding(DEFLATE);
                }
            }

            if (System.IO.File.Exists(filepath))
                context.Response.WriteFile(filepath);
        }
        #endregion


    }
}